www.gusucode.com > matlab从零到进阶程序与数据 > matlab从零到进阶程序与数据/第5章 图形用户界面(GUI)编程/ex54_13.m

    function ex54_13
%使用缺省属性值创建图形窗口对象
s.hf=figure;
%设置图形窗口对象的部分属性值
set(s.hf,...
    'toolbar','figure',...
    'menubar','none',...
    'numbertitle','off',...
    'name','example window',...
    'units','normalized',...
    'position',[0.3 0.4 0.6 0.5]);
%创建坐标轴对象
s.haxes=axes('parent',s.hf,'position',[0.1 0.1 0.5 0.8]);
%初始化绘图数据
x=0:pi/50:2*pi;
y=sin(x);
%指定当前坐标轴
axes(s.haxes);
%绘制图形
s.plot=plot(x,y,'r','linewidth',1.5);
%设置坐标轴的文本为倾斜,x轴的颜色为红色
set(gca,'fontangle','italic','xcolor',[1 0 0]);
%打开栅格
grid on
%给坐标轴对象添加标题
title('我建立的第一个坐标轴');
%给x和y轴添加标签
xlabel('x坐标轴');
ylabel('y坐标轴');
s.hgroup=uibuttongroup('parent',s.hf,...
    'title','栅格控制',...
    'units','normalized',...
    'position',[0.7 0.7 0.1 0.2]);
s.hradio1=uicontrol('parent',s.hgroup,...
    'style','radiobutton',...
    'tag','radiobutton1',...
    'string','grid on',...
    'units','normalized',...
    'position',[0.1 0.7 0.8 0.2]);
s.hradio2=uicontrol('parent',s.hgroup,...
    'style','radiobutton',...
    'tag','radiobutton2',...
    'string','grid off',...
    'units','normalized',...
    'position',[0.1 0.1 0.8 0.2]);
s.text1=uicontrol('parent',s.hf,...
    'style','text',...
    'string','曲线颜色',...
    'units','normalized',...
    'position',[0.7 0.6 0.1 0.05]);
s.list=uicontrol('parent',s.hf,...
    'style','listbox',...
    'string',{'r(红色)';'g(绿色)';'b(蓝色)'},...
    'units','normalized',...
    'position',[0.805 0.55 0.1 0.1]);
s.text2=uicontrol('parent',s.hf,...
    'style','text',...
    'string','曲线线型',...
    'units','normalized',...
    'position',[0.7 0.45 0.1 0.05]);
s.pop=uicontrol('parent',s.hf,...
    'style','popupmenu',...
    'string',{'-';'--';':';'-.';'none'},...
    'units','normalized',...
    'position',[0.805 0.4 0.1 0.1]);
set(s.hgroup,'SelectionChangeFcn',...
    {@buttongroup_SelectionChangeFcn,s});
set(s.list,'callback',{@list_callback,s});
set(s.pop,'callback',{@pop_callback,s});

function buttongroup_SelectionChangeFcn(hObject,...
    eventdata,s)
tag= get(eventdata.NewValue,'Tag');
switch tag
    case 'radiobutton1'
        set(s.haxes,'xgrid','on','ygrid','on');
    case 'radiobutton2'
        set(s.haxes,'xgrid','off','ygrid','off');
end

function list_callback(hObject,...
    eventdata,s)
value= get(hObject,'value');
switch value
    case 1
        set(s.plot,'color','r');
    case 2
        set(s.plot,'color','g');
    case 3
        set(s.plot,'color','b');
end

function pop_callback(hObject,...
    eventdata,s)
value= get(hObject,'value');
switch value
    case 1
        set(s.plot,'linestyle','-','marker','none');
    case 2
        set(s.plot,'linestyle','--','marker','none');
    case 3
        set(s.plot,'linestyle',':','marker','none');
    case 4
        set(s.plot,'linestyle','-.','marker','none');
    case 5
        set(s.plot,'linestyle','none','marker','none');
end